﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 求数组的最大值
{
    public delegate int DelCompare(object o1, object o2);
    class Program
    {
        static void Main(string[] args)
        {
            object[] nums = {1,2,3,6,5,7,34,65 };
            object[] names = {"王小帅","尼古拉斯赵四","刘能","哈哈","代码改变世界!" };
            object[] pers = { new Person() { Name = "张三", Age = 19 }, new Person {Name= "尼古拉斯赵四", Age=33 },new Person {Name="王小波试试",Age=80 } };
            //object max = GetMax(pers, delegate (object o1, object o2) { return ((Person)o1).Age - ((Person)o2).Age; });
            object max = GetMax(pers, (o1, o2) => { return ((Person)o1).Age - ((Person)o2).Age; });
            //Console.WriteLine(GetMax(nums, C1));
            //Console.WriteLine(GetMax(names, C2));
            Console.WriteLine(((Person)max).Name);
            Console.ReadKey();
        }

        static object GetMax(object[] nums,DelCompare del)
        {
            object max = nums[0];
            for (int i = 0; i < nums.Length; i++)
            {
                if(del(max,nums[i])<0)//传入比较方式
                {
                    max = nums[i];
                }
            }
            return max;
        }

        static int C1(object o1,object o2)
        {
            int n1 = (int)o1;
            int n2 = (int)o2;
            return n1 - n2;
        }

        static int C2(object o1,object o2)
        {
            int n1 = o1.ToString().Length;
            int n2 = o2.ToString().Length;
            return n1 - n2;
        }


    }

    class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}
